using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace Magenic.CodeMastery.SharePoint.PMO
{
    public class TableBasedRow
    {
        private DisplayPeriod display = null;

        private int index = 0;

        private StringBuilder html = new StringBuilder();

        public TableBasedRow(DisplayPeriod period)
        {
            if (period == null)
                throw new ArgumentNullException();
            this.display = period;
        }

        public void Add(Project project)
        {
            int startColumn = display.StartColumn(project) - 1;
            if (startColumn < index)
                throw new ArgumentOutOfRangeException(
                    "start column overlaps with existing index");

            if (startColumn > index)
                renderTd(startColumn - index, null, null, null, null);


            int finishColumn = display.FinishColumn(project) - 1;
            int width = finishColumn - startColumn + 1;

            String text = composeProjectBarText(project);
            String title = composeMouseOverText(project);
            renderTd(width, "prj-bar", null, text, title);
        }

        /// <summary>
        /// Composes the mouse-over text displayed for the project bar.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        private String composeMouseOverText(Project project)
        {
            String text = String.Format("Runs from {0} through ({1})",
                project.Start, project.Finish);
            return text;
        }

        /// <summary>
        /// Composes the text displayed in the project bar.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        private String composeProjectBarText(Project project)
        {
            return project.Name;
        }

        public void AddTrailingColumns()
        {
            if (index < display.Columns)
                renderTd(display.Columns - index, null, null, null, null);
        }

        public override String ToString()
        {
            if (html.Length == 0)
                return String.Empty;
            return String.Format("<tr>{0}</tr>", html);
        }

        public void renderTd(int width, String cssClass, String style,
            String text, String title)
        {
            if (width <= 0)
                throw new ArgumentOutOfRangeException(String.Format(
                    "invalid width: {0} <= 0", width));


            html.Append(String.Format("<td colspan=\"{0}\"", width));

            title = (title ?? String.Empty).Trim();
            if (title.Length > 0)
                html.Append(String.Format(" title=\"{0}\"",
                    HttpUtility.HtmlEncode(title)));

            cssClass = (cssClass ?? String.Empty).Trim();
            if (cssClass.Length > 0)
                html.Append(String.Format(" class=\"{0}\"", cssClass));

            index += width;

            //NOTE: The following 'breaks' String.Format due to the double
            //open curlys.
            if (style != null && style.Length != 0)
            {
                html.Append(" style=\"{");
                html.Append(style);
                html.Append("}\"");
            }

            html.Append(">");

            if (text != null && text.Length != 0)
                html.Append(HttpUtility.HtmlEncode(text));

            html.Append("</td>");
        }

        private TableBasedRow() { }
    }
}